package com.java2302.manufacturing.service.impl;


import com.java2302.manufacturing.dao.SysUserDao;
import com.java2302.manufacturing.dto.UserDTO;
import com.java2302.manufacturing.entity.SysPermission;
import com.java2302.manufacturing.entity.SysUser;
import com.java2302.manufacturing.service.UserService;
import com.java2302.manufacturing.vo.UserVo;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.java2302.manufacturing.entity.table.SysUserTableDef.SYS_USER;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Resource
    SysUserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {


        SysUser loginuser = userDao.selectOneByQuery(new QueryWrapper().where("username = ?" ,username));
        if(loginuser == null){
            throw new UsernameNotFoundException("用户不存在");
        }
        return new UserDetails() {
            @Override
            public Collection<? extends GrantedAuthority> getAuthorities() {
                List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
                UserDTO sysUserDtoList = findUserWithPermissions(username);
                for (SysPermission permission : sysUserDtoList.getSysPermission()) {
                    GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(permission.getPermUrl());
                    grantedAuthorityList.add(grantedAuthority);
                }
                return grantedAuthorityList;
            }

            @Override
            public String getPassword() {
                return loginuser.getPassword();
            }

            @Override
            public String getUsername() {
                return loginuser.getUsername();
            }

            @Override
            public boolean isAccountNonExpired() {
                return true;
            }

            @Override
            public boolean isAccountNonLocked() {
                return true;
            }

            @Override
            public boolean isCredentialsNonExpired() {
                return true;
            }

            @Override
            public boolean isEnabled() {
                return true;
            }
        };
    }

    @Override
    public void addUser(UserVo userVo) {
        SysUser user = new SysUser();

        user.setUsername(userVo.getUsername());
        user.setPassword(new BCryptPasswordEncoder().encode(userVo.getPassword()));
        user.setNickname(user.getUsername());
        userDao.insert(user);
        log.debug("插入用户:"+user.toString());
    }

    @Override
    public List<SysUser> query() {
        return userDao.selectAll();
    }

    @Override
    public UserDTO findUserWithPermissions(String username) {
        QueryWrapper queryWrapper = new QueryWrapper().from(SYS_USER)
                .leftJoin(" sys_user_role")
                .on("sys_user.user_id = sys_user_role.rid")
                .leftJoin("sys_role")
                .on("sys_user_role.rid = sys_role.rid")
                .leftJoin("sys_role_permission")
                .on("sys_role.rid = sys_role_permission.role_id")
                .leftJoin("sys_permission")
                .on("sys_role_permission.permission_id = sys_permission.perm_id")
                .where("sys_user.username = ?",username);
        UserDTO DtoList = userDao.selectOneByQueryAs(queryWrapper,UserDTO.class);
        return DtoList;
    }
}
